package com.pompom.matchcrawler;

import gummy.database.GummyClient;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

public class MPBuffer {

	  HashMap<Class, LinkedList<Object>> map = new HashMap<Class,LinkedList<Object>>();

	public     int BUFFER_SIZE = 200;
	public   void insertDelayed (Object o)
	{

		LinkedList<Object> array = map.get(o.getClass());

		if (array== null)
		{
			array = new LinkedList<Object>();
			System.out.println("created map for "+o.getClass());
			map.put(o.getClass(), array);
		}

		array.add(o);


		if (array.size()>BUFFER_SIZE)
		{
			flushArray(array, o.getClass());
		}


	}


	private static void flushArray(LinkedList<Object> array,Class c)
	{ 
		Object objArray[] =null;

		objArray  = array.toArray();
		array.clear();



		try
		{
			GummyClient.getInstance("DT2").executeInsertTO(objArray);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}  

	}
	{

	}

	public    void flushAll()
	{
		Iterator<Class> i = map.keySet().iterator();

		while (i.hasNext())
		{
			Class c = i.next();


			LinkedList<Object> ll = map.get(c);
			flushArray(ll, c);


		}
		System.out.println("Flushed.");


	}



}
